const { Controller } = require('egg');
const validate = require('../rule/rule');

class UserController extends Controller {
  /**
   * @param req
   * @param res
   * @api {GET} /userManage/user/info 获取用户详情
   * @apiName info
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiQuery  { String } id 用户id
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async info(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { id } = await validate.userInfoValidate(query);

      const data = await ctx.service.user.info(id);

      ctx.status = 200;
      // ctx.sendSuccess({ data });
      ctx.body = { code: 200, data, msg: 200 };
      return;
    } catch (error) {
      ctx.status = 200;
      console.log(error);
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api { POST } /userManage/user/list 获取用户列表
   * @apiName list
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } name 用户名
   * @apiParam { String } login_name 登录名
   * @apiParam { Number } sex 性别
   * @apiParam { String } method 排序方式
   * @apiParam { String } order 排序字段
   * @apiParam { Number } pageIndex 页码
   * @apiParam { Number } pageSize 页码容量
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async list(req, res) {
    const ctx = this.ctx;
    try {
      const { query } = req;
      const { name, login_name, sex, method, order, pageIndex, pageSize } = validate.userListValidate(query);

      const data = await ctx.service.user.list(name, login_name, sex, method, order, pageIndex, pageSize);
      ctx.body = {
        code: 200,
        msg: 'success',
        data,
      };
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.body = {
        code: 500,
        msg: error.message,
      };
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api { POST } /userManage/user/insert 新增用户
   * @apiName insert
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam {String} name 用户名称
   * @apiParam {String} login_name 登录名称
   * @apiParam {Number} sex 性别
   * @apiParam {String} password 密码
   * @apiParam {String} phone 用户电话号码
   * @apiParam {String} email 用户
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async insert(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { name, login_name, sex, password, phone, email } = await validate.userInsertValidate(body);
      const data = await ctx.service.user.insert(name, login_name, sex, password, phone, email);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api { POST } /userManage/user/update 更新用户
   * @apiName update
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   * @apiRequest json
   * @apiParam { String } id 用户id
   * @apiParam { String } name 用户名称
   * @apiParam { Number } sex 性别
   * @apiParam { String } phone 用户电话
   * @apiParam { String } email 用户邮箱
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async update(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { id, name, sex, phone, email } = await validate.userUpdateValidate(body);
      const data = await ctx.service.user.update(id, name, sex, phone, email);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @param req
   * @param res
   * @api { POST } /userManage/user/delete 用户删除
   * @apiName delete
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   *
   * @apiRequest json
   * @apiParam { Object } ids 用户ids
   *
   * @apiHeader { String } Authorization 注册信息
   */
  async delete(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { ids } = await validate.userDeleteValidate(body);
      const data = await ctx.service.user.delete(ids);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      console.log(error);
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api { POST } /userManage/user/forgetPassword 用户删除
   * @apiName forgetPassword
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   *
   * @apiRequest json
   * @apiParam { Steing } newPassword 新密码
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async forgetPassword(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;
      const { newPassword } = await validate.forgetPasswordValidate(body);
      const data = await ctx.service.user.forgetPassword(newPassword);
      ctx.status = 200;
      ctx.sendSuccess({ data });

      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  /**
   * @api { POST } /userManage/user/forgetPassword 用户删除
   * @apiName forgetPassword
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   *
   * @apiRequest json
   * @apiParam { Steing } newPassword 新密码
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async changePassword(req, res) {
    const ctx = this.ctx;
    try {
      const { body } = req.request;

      const { newPassword, user_id } = await validate.changePasswordValidate(body);

      const data = await ctx.service.user.changePassword(newPassword, user_id);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }

  /**
   * @api { POST } /userManage/user/register 注册
   * @apiName register
   * @apiGroup user
   * @apiVersion 1.0.0
   *
   *
   * @apiRequest json
   * @apiParam { Steing } newPassword 新密码
   *
   * @apiHeader { String } Authorization 注册信息
   */

  async register() {
    const ctx = this.ctx;
    try {
      const { body } = ctx.request;

      const { name, login_name, password } = await validate.register(body);

      const data = await ctx.service.user.register(name, login_name, password);
      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }


  async init() {
    const ctx = this.ctx;
    try {
      const data = await ctx.service.user.init();

      ctx.status = 200;
      ctx.sendSuccess({ data });
      return;
    } catch (error) {
      console.log(error);
      ctx.status = 200;
      ctx.sendFailed({ msg: error.message });
      return;
    }
  }
}

module.exports = UserController;
